package com.ghsc.ghxc.jsb.ui.login

import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.widget.LinearLayout
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.asColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.startKtxActivity
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityAgreementBinding
import com.just.agentweb.*
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.api.getUserIdRidUrl
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.ui.home.activity.WebViewActivity

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.login.activity
 * 类名：AgreementActivity
 * 创建时间：2023/4/7 16:09
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class AgreementActivity  : BaseVMActivity<AgreementViewModel, ActivityAgreementBinding>() {
    override fun attachVMClass(): Class<AgreementViewModel>? {
        return AgreementViewModel::class.java
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_agreement
    }

    var bundle: Bundle? = null
    var sign: Int = 0


    var agentWeb: AgentWeb? = null


    val config = UserManager.getConfig()

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    //k8是隐私政策
    //k9用户协议
    override fun initView() {
        bundle = intent.extras
        sign = bundle!!.getInt("sign")
        if (sign==3)
            binding.bar.topTitle.text = "服务协议"
        else if (sign == 1)
            binding.bar.topTitle.text = "用户协议"
        else binding.bar.topTitle.text = "隐私协议"
        config?.let {
            var url = ""
            var sysConfigResultData = it.getSysConfigBean(if(sign==1) CustKey.configK9 else CustKey.configK8 )
            if(sysConfigResultData!=null)
                if(!TextUtils.isEmpty(sysConfigResultData.cfg_value)){
                    url = sysConfigResultData.cfg_value
                }
            loge("xx=" + url)
            agentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.llContainer, LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(R.color.main_color.asColor(), 2)
                .setWebChromeClient(webChromeClient)
                .setWebViewClient(webViewClient)
                .setAgentWebWebSettings(AgentWebSettingsImpl.getInstance())
                .setMainFrameErrorView(
                    R.layout.layout_agentweb_error_page,
                    -1
                ) //参数1是错误显示的布局，参数2点击刷新控件ID -1表示点击整个布局都刷新
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
//            .setWebLayout(WebLayout(this))
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK) //打开其他应用时，弹窗咨询用户是否前往其他应用
                .interceptUnkownUrl() //拦截找不到相关页面的Url
                .createAgentWeb()
                .ready()
                .go(url)
        }

        binding.run {
            bar.topTitle.text = ""
            bar.topLeftView.setOnClickListener {
                if (!agentWeb!!.back()) {
                    finish()
                }
            }
        }
        //设置webView滑动模式
        agentWeb!!.webCreator.webView.overScrollMode = WebView.OVER_SCROLL_NEVER
        //js交互  暂时没用到。
//        agentWeb.jsInterfaceHolder.addJavaObject("android", AndroidInterface())
        //调用H5页面的方法示例,quickCallJs方法传参是js定义的方法名字
        //https://github.com/Justson/AgentWeb/blob/androidx/sample/src/main/java/com/just/agentweb/sample/fragment/JsAgentWebFragment.java
        ////agentWeb.jsAccessEntrace.quickCallJs("")
    }

    private val webChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
            binding.bar.topTitle.text = title
        }
    }
    private val webViewClient: WebViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
            return super.shouldOverrideUrlLoading(view, request)
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (agentWeb!!.handleKeyEvent(keyCode, event)) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        agentWeb!!.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        agentWeb!!.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        //清空缓存
        AgentWebConfig.clearDiskCache(this)
        agentWeb!!.webLifeCycle.onDestroy()
        agentWeb!!.clearWebCache()
    }
//    //这里是和webveiw做交互的 暂时没有用到。
//    inner class AndroidInterface {
//        // 邀请好友
//        @JavascriptInterface
//        fun invite_friend(param: String?) {
//            runOnUiThread {
////                mViewModel.getShareInfo()
//            }
//        }
//
//    }

}

